package com.lzj.demo.design.observer;

import java.util.ArrayList;
import java.util.List;

public class Observer {
    private List<Iobserver> observers = new ArrayList<>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        notifyAllObservers(state);
    }

    //添加观察者
    public void addServer(Iobserver observer){
        observers.add(observer);
    }

    //移除观察者
    public void removeServer(Iobserver observer){
        observers.remove(observer);
    }

    //通知
    public void notifyAllObservers(int state){
        if(state!=1){
            System.out.println("不是通知的状态");
            return ;
        }
        for (Iobserver observer : observers) {
            observer.doEvent();
        }
    }
}
